<?php
$host = "https://ali-todayoil.showapi.com";
$path = "/todayoil";
$method = "GET";
$appcode = "17691e95433548018a5559b338b4b2ab";
$headers = array();
array_push($headers, "Authorization:APPCODE " . $appcode);
$bodys = "";
$url = $host . $path;
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, false);
if (1 == strpos("$".$host, "https://"))
{
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
echo(curl_exec($curl));
$data = json_decode(urldecode(curl_exec($curl)));
$result = $data->showapi_res_body;//获得result数组
$list = $result->list;


$mysql_server_name = 'bdm30289510.my3w.com'; //改成自己的mysql数据库服务器
$mysql_username = 'bdm30289510'; //改成自己的mysql数据库用户名
$mysql_password = 'ali123sql'; //改成自己的mysql数据库密码
$mysql_database = 'bdm30289510_db'; //改成自己的mysql数据库名
$con = mysql_connect($mysql_server_name, $mysql_username, $mysql_password);
if (!$con) {
  die('Could not connect: ' . mysql_error());
}
//设置数据的字符集utf-8 
mysql_query("set names 'utf8' ");
mysql_query("set character_set_client=utf8");
mysql_query("set character_set_results=utf8");
mysql_select_db($mysql_database, $con);


foreach ($list as $value) {
  //处理数据
  $prov=$value->prov;
  $day=substr($value->ct,0,10);
  $p92=$value->p92;
  $p93=$value->p93;
  $p95=$value->p95;
  $p97=$value->p97;
  $sql="insert `todayoil`(`prov`,`day`,`p92`,`p93`,`p95`,`p97`) values('$prov','$day','$p92','$p93','$p95','$p97');";
  $result = mysql_query($sql);  
}


$jobj=new stdclass();//实例化stdclass，这是php内置的空类，可以用来传递数据，由于json_encode后的数据是以对象数组的形式存放的，
$jobj->success=true;

echo json_encode($jobj);//打印编码后的json字符串
?>